/*************************************************************
 * @file motor.h
 * @author MicroOrange
 * @brief 
 * @date 2023/2/18
 *************************************************************/

#ifndef LS_ARM_AIM_484_H
#define LS_ARM_AIM_484_H
#include "sorg_utils.h"
#include "stm32f1xx_hal.h"

#define MOTOR_MAX_BUFFER_LENGTH 30
#define MOTOR_UART_TIMEOUT 1000

typedef struct {
    UART_HandleTypeDef *uart;
    uint8_t dev_addr;
    uint8_t tx_buffer[MOTOR_MAX_BUFFER_LENGTH];
    uint8_t rx_buffer[MOTOR_MAX_BUFFER_LENGTH];
} motor_aim;


#define AIM_REG_MODBUS_ENABLE 0x00
#define AIM_REG_OUTPUT_ENABLE 0x01
#define AIM_REG_TARGET_SPEED 0x02
#define AIM_REG_TARGET_ACC 0x03
#define AIM_REG_MAGNET_ANGLE 0x04
#define AIM_REG_SPEED_PID_P 0x05
#define AIM_REG_SPEED_PID_I 0x06
#define AIM_REG_POSITION_PID_P 0x07
#define AIM_REG_SPEED_FEEDBACK 0x08
#define AIM_REG_DIR_POLAR 0x09
#define AIM_REG_GEAR_NUMERATOR 0x0A
#define AIM_REG_GEAR_DENOMINATOR 0x0B
#define AIM_REG_TARGET_POSITION_L 0x0C
#define AIM_REG_TARGET_POSITION_H 0x0D
#define AIM_REG_ALARM_CODE 0x0E
#define AIM_REG_CURRENT 0x0F
#define AIM_REG_SPEED 0x10
#define AIM_REG_TEMPERATURE 0x11
#define AIM_REG_PWM 0x13
#define AIM_REG_PARA_SAVE 0x14
#define AIM_REG_DEV_ADDR 0x15
#define AIM_REG_ABS_POSITION_L 0x16
#define AIM_REG_ABS_POSITION_M 0x17
#define AIM_REG_OUTPUT_MAX_DZ 0x18
#define AIM_REG_SPECIAL 0x19


#define AIM_COMMAND_READ_REG 0x03
#define AIM_COMMAND_WRITE_REG 0x06
#define AIM_COMMAND_WRITE_PU 0x10

bool aim_write_reg(motor_aim *motor, uint8_t addr, uint16_t val);
uint16_t aim_read_reg(motor_aim *motor, uint8_t addr);
void aim_motor_init(motor_aim *motor,UART_HandleTypeDef *uart, uint8_t dev_addr);
void aim_write_pu(motor_aim *motor, uint32_t val, uint8_t addr);

#endif //LS_ARM_AIM_484_H
